<?php

declare(strict_types=1);
/**
 * This file is part of bearing-web.
 *
 * @link     https://www.bearing.cn/
 * @contact  tech@bearing.cn
 * @license  https://gitee.com/cppo/bearing_web/-/blob/master/README.md
 */

namespace common\models;

use yii\behaviors\TimestampBehavior;
use yii\db\ActiveRecord;
use yii\db\BaseActiveRecord;

/**
 * This is the model class for table "{{%user_message}}".
 *
 * @property int $id
 * @property int $user_id
 * @property string $first_name
 * @property string $last_name
 * @property string $email
 * @property string $subject
 * @property string $message
 * @property int $status
 * @property int $create_time
 * @property int $update_time
 */
class UserMessageModel extends ActiveRecord
{
    public static function tableName(): string
    {
        return '{{%user_message}}';
    }

    public function rules(): array
    {
        return [
            [['update_time'], 'default', 'value' => 0],
            [['subject'], 'default', 'value' => ''],
            [['user_id', 'status', 'create_time', 'update_time'], 'integer'],
            [['message'], 'required'],
            [['message'], 'string'],
            [['first_name', 'last_name'], 'string', 'max' => 50],
            [['email', 'subject'], 'string', 'max' => 255],
        ];
    }

    public function attributeLabels(): array
    {
        return [
            'id' => 'ID',
            'user_id' => 'User ID',
            'first_name' => 'First Name',
            'last_name' => 'Last Name',
            'email' => 'Email',
            'subject' => 'Subject',
            'message' => 'Message',
            'status' => 'Status',
            'create_time' => 'Create Time',
            'update_time' => 'Update Time',
        ];
    }

    /**
     * 时间更新策略.
     *
     * @return array[]
     */
    public function behaviors(): array
    {
        return [
            [
                'class' => TimestampBehavior::class,
                'attributes' => [
                    BaseActiveRecord::EVENT_BEFORE_INSERT => ['create_time', 'update_time'],   // 创建
                    BaseActiveRecord::EVENT_BEFORE_UPDATE => ['update_time'],       // 修改
                ],
                'value' => function () {
                    return time();
                },
            ],
        ];
    }
}
